package com.example.mybatisdemo.service;

import com.example.mybatisdemo.utils.FileDown;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;

@Service
public class PdfService {
    public void exportPdf(HttpServletResponse response) throws Exception {
        //新建一个A4大小的文档
        Document document = new Document(PageSize.A4);

//创建一个ByteArrayOutputStream绑定document，后续只需要将数据流写入baos即可
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfWriter.getInstance(document, baos);

//打开文档
        document.open();

        // 设置中文字体
        BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

//文档标题字体样式
        Font titleFontChinese = new Font(bfChinese, 15, Font.NORMAL);

//小标题字体样式
        Font smallTitleFontChinese = new Font(bfChinese, 12, Font.NORMAL);

//表头字体样式
        Font headFontChinese = new Font(bfChinese, 12, Font.NORMAL);

//内容字体样式
        Font contentFontChinese = new Font(bfChinese, 9, Font.NORMAL);

        //这一步将step2中的titleFontChinese设置进来即可解决中文字体不显示问题
        Paragraph title = new Paragraph("燃气预警", titleFontChinese);

//标题居中
        title.setAlignment(Element.ALIGN_CENTER);

//将标题添加到文档中
        document.add(title);



        //保持美观，空一行
        document.add(new Paragraph(" "));
        //设置小标题
        document.add(new Paragraph("反馈内容", smallTitleFontChinese));
        document.add(new Paragraph("环境温度较高，对储罐进行喷淋降温", contentFontChinese));

       //保持美观，空一行
        document.add(new Paragraph(" "));

//设置小标题
        document.add(new Paragraph("附件", smallTitleFontChinese));

// 添加第一张图片到PDF
        String url = "http://172.22.168.84//sqsyzlsjfbOss/6e087a79-69ad-4916-b8b0-26bfe73677a4.jpg";
        String fileName =url.substring(url.lastIndexOf('/') + 1);
        String downloadDir = System.getProperty("user.dir");
        //下载临时文件至本地，在上传之后删除
        File file = FileDown.downloadFile(url, downloadDir,fileName);
        MultipartFile multipartFile = FileDown.fileToMultipartFile(file);
        Image img1 = Image.getInstance(multipartFile.getBytes());
        file.delete();

//重新设置图片大小，适配PDF
        img1.scaleToFit(525, img1.getHeight());

//将图片添加进PDF
        document.add(img1);

//关闭文档
        document.close();
        String pdfPath = downloadDir + File.separatorChar + "export.pdf";
        File pdfFile = new File(pdfPath);
        FileOutputStream fileOutputStream = new FileOutputStream(pdfFile);
        baos.writeTo(fileOutputStream);

//        //设置响应头和内容格式
//        response.setContentType("application/pdf");
//        response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=export.pdf");
//
//
//        //将PDF内容写入响应输出流并关闭
//        ServletOutputStream outputStream = response.getOutputStream();
//        outputStream.write(baos.toByteArray());

        fileOutputStream.flush();
        fileOutputStream.close();
    }
}
